﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using AntUnion.FrameWork.Model.Models;
using Util.Extension;
using Microsoft.AspNetCore.Hosting;
using AntUnion.FrameWork.Util;
using Microsoft.AspNetCore.Mvc.Rendering;
using AntUnion.FrameWork.Web.Extend;

namespace AntUnion.FrameWork.Web.Areas.Console.Controllers
{
    [Area("Console")]
    public class PersonInfoController : BaseController
    {
        private readonly _antunionContext _context;

        public PersonInfoController(_antunionContext context, IWebHostEnvironment env) : base(context, env)
        {
            _context = context;
        }

        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取身份证信息列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <param name="personname"></param>
        /// <param name="idcard"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetListJson(int page, int limit, string name, string personname, string idcard)
        {
            ListResult res = new ListResult();
            try
            {

                var data = from person in _context.Personinfos
                           select new
                           {
                               id = person.Id,
                               name = person.Number,
                               personname = person.Personname,
                               idcard = person.Idcard,
                               type = person.Type,
                               addtime = person.Addtime,
                               updatetime = person.Updatetime
                           };
                if (!string.IsNullOrEmpty(name))
                {
                    data = data.Where(k => k.name.Contains(name));
                }
                if (!string.IsNullOrEmpty(personname))
                {
                    data = data.Where(k => k.name.Contains(personname));
                }
                if (!string.IsNullOrEmpty(idcard))
                {
                    data = data.Where(k => k.idcard.Contains(idcard));
                }
                res.Data = await data.OrderBy(k => k.id).Skip((page - 1) * limit).Take(limit).ToListAsync();
                res.Code = 0;
                res.Count = data.Count();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 删除人员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> Delete(int id)
        {
            ListResult res = new ListResult();
            try
            {
                Personinfo personinfo = await _context.Personinfos.FindAsync(id);
                _context.Personinfos.Remove(personinfo);
                res.Code = 0;
                res.Data = await _context.SaveChangesAsync();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 清空人员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> DeleteAll()
        {
            ListResult res = new ListResult();
            try
            {
                _context.Database.ExecuteSqlRaw("delete from personinfo");
                res.Code = 0;
                res.Data = await _context.SaveChangesAsync();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }
    }
}
